<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Log;
/**
 * 管理员管理
 * Class AdminUser
 * @package app\admin\controller
 */
class Hsadmin extends AdminBase
{
    protected function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 管理员管理
     * @return mixed
     */
    public function index()
    {
        return $this->fetch('index', ['admin_user_list' => db('admin_user')->order('id')->select()]);
    }

    /**
     * 添加管理员
     * @return mixed
     */
    public function add()
    {
        return $this->fetch('add', ['auth_group_list' => db('auth_group')->order('id')->select()]);
    }

    /**
     * 保存管理员
     * @param $group_id
     */
    public function save($group_id)
    {
        if ($this->request->isPost()) {
            $data = $this->request->except('confirm_password','post');
            if($data['password'] != input('confirm_password')) $this->error('两次密码输入不一致');
            if(db('admin_user')->where(['username'=>$data['username']])->find()) $this->error('管理员名称已存在');
            $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
            //unset($data['confirm_password'],$data['group_id']);//去除非数据表字段数据
            $data['create_time'] = time();
            //allowField(true),过滤post数组中的非数据表字段数据,仅继承model类后可用
            //insertGetId新增数据并返回主键值
            if (db('admin_user')->insert($data)) {  
                $this->success('保存成功','index');
            } else {
                $this->error('保存失败');
            }
        }
    }

    /**
     * 编辑管理员
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $admin_user             = db('admin_user')->find($id);
        $auth_group_list        = db('auth_group')->order('id')->select();

        return $this->fetch('edit', ['admin_user' => $admin_user, 'auth_group_list' => $auth_group_list]);
    }

    /**
     * 更新管理员
     * @param $id
     * @param $group_id
     */
    public function update($id, $group_id)
    {
        if ($this->request->isPost()) {
            $data            = input('post.');
            $admin_user = db('admin_user')->find($id);
            $admin_data['id']               = $admin_user['id'];
            $admin_data['username']         = $data['username'];
            $admin_data['status']           = $data['status'];
            $admin_data['name']           = $data['name'];
            $admin_data['group_id']           = $data['group_id'];
            if(db('admin_user')->where(['username'=>$data['username'],'id'=>['<>',$id]])->find()) $this->error('管理员名称已存在');
            if (!empty($data['password']) && !empty($data['confirm_password'])) {
                if($data['password'] != $data['confirm_password']) $this->error('两次密码输入不一致');
                $admin_data['password']     = password_hash($data['password'], PASSWORD_DEFAULT);
            }

            if (db('admin_user')->update($admin_data) !== false) {
                $this->success('更新成功','index');
            } else {
                $this->error('更新失败');
            }
        }
    }

    /**
     * 删除管理员
     * @param $id
     */
    public function delete($id)
    {
        if ($id == 1) {
            $this->error('默认管理员不可删除');
        }
        if (db('admin_user')->delete($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * 权限组
     * @return mixed
     */
    public function group_index()
    {
        return $this->fetch('group_index', ['auth_group_list' => db('auth_group')->select()]);
    }
    /*添加权限组*/
    public function group_add(){
        return $this->fetch('group_add');
    }
    //保存权限组
    public function group_save(){
        if ($this->request->isPost()) {
            $data = input('post.');

            if (db('auth_group')->insert($data) !== false) {
                $this->success('保存成功','group_index');
            } else {
                $this->error('保存失败');
            }
        }
    }
    /*编辑权限组*/
    public function group_edit($id){
        return $this->fetch('group_edit', ['auth_group' => db('auth_group')->find($id)]);
    }
    //更新权限组
    public function group_update($id){
        if ($this->request->isPost()) {
            $data = input('post.');

            if ($id == 1 && $data['status'] != 1) {
                $this->error('超级管理组不可禁用');
            }
            if (db('auth_group')->update($data) !== false) {
                $this->success('更新成功','group_index');
            } else {
                $this->error('更新失败');
            }
        }
    }
    //删除权限组
    public function group_delete($id){
        if ($id == 1) {
            $this->error('超级管理组不可删除');
        }
        if (db('auth_group')->delete($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
    /*授权*/
    public function auth($id){
        return $this->fetch('auth', ['id' => $id]);
    }
    /**
     * AJAX获取规则数据
     * @param $id
     * @return mixed
     */
    public function getJson($id)
    {
        $auth_group_data = db('auth_group')->find($id);
        $auth_rules      = explode(',', $auth_group_data['rules']);
        $auth_rule_list  = db('auth_rule')->field('id,pid,title')->order('sort desc,id')->select();

        foreach ($auth_rule_list as $key => $value) {
            in_array($value['id'], $auth_rules) && $auth_rule_list[$key]['checked'] = true;
        }
        return $auth_rule_list;
    }
    /**
     * 更新权限组规则
     * @param $id
     * @param $auth_rule_ids
     */
    public function updateAuthGroupRule($id, $auth_rule_ids = '')
    {
        if ($this->request->isPost()) {
            if ($id) {
                $group_data['id']    = $id;
                $group_data['rules'] = is_array($auth_rule_ids) ? implode(',', $auth_rule_ids) : '';

                if (db('auth_group')->update($group_data) !== false) {
                    $this->success('授权成功');
                } else {
                    $this->error('授权失败');
                }
            }
        }
    }
}